package com.uniledger.exchange.home;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sunfusheng.marqueeview.MarqueeView;
import com.uniledger.commonlib.view.FullListView;
import com.uniledger.exchange.R;
import com.uniledger.exchange.home.scheme.HomeAssetAdapter;
import com.uniledger.exchange.model.OkSingleton;
import com.uniledger.exchange.model.StellarAsset;
import com.yqritc.recyclerviewflexibledivider.FlexibleDividerDecoration;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ch.ielse.view.SwitchView;
import es.dmoral.prefs.Prefs;

/**
 * 首页适配器
 */
public class HomeCenterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements FlexibleDividerDecoration.VisibilityProvider,
//        FlexibleDividerDecoration.PaintProvider,
        HorizontalDividerItemDecoration.MarginProvider {

    private List<HomeCenter> centerList;
    private Activity context;
    private LayoutInflater inflater;

    private HeadListener headListener;
    private List<StellarAsset> stellarAssets;

    public void setHeadListener(HeadListener headListener) {
        this.headListener = headListener;
    }

    public HomeCenterAdapter(Activity context) {
        this.context = context;
        this.centerList = new ArrayList<>();
        this.inflater = LayoutInflater.from(context);
        centerList.add(new HomeCenter(HomeCenter.TYPE_HOME_HEAD));

        stellarAssets = new ArrayList<>();
        centerList.add(new HomeCenter(stellarAssets));
        centerList.add(new HomeCenter(getStr(R.string.home_scheme_transfer), R.drawable.ic_home_transfer, HomeCenter.SCHEME_TRANSFER));
        centerList.add(new HomeCenter(getStr(R.string.home_scheme_exchange), R.drawable.ic_home_exchange, HomeCenter.SCHEME_EXCHANGE));
        centerList.add(new HomeCenter(getStr(R.string.home_scheme_topup), R.drawable.ic_home_top_up, HomeCenter.SCHEME_TOP_UP));
        centerList.add(new HomeCenter(getStr(R.string.home_scheme_withdraw), R.drawable.ic_home_withdraw, HomeCenter.SCHEME_WITHDRAW));

    }

    private String getStr(int id){
        return context.getResources().getString(id);
    }

    public void updateAsset(List<StellarAsset> assets) {
        if (assets == null || assets.isEmpty())
            return;
        stellarAssets.clear();
        stellarAssets.addAll(assets);
        notifyDataSetChanged();

        // 设置全局资产类型及余额
        StellarAsset asset= assets.get(0);
        OkSingleton.getInstance().setAssetCurrency(asset.currency);
        OkSingleton.getInstance().setAssetBalance(asset.amount);
    }

    @Override
    public int getItemViewType(int position) {
        HomeCenter center = centerList.get(position);
        return center.type;

    }

    public HomeCenter getItemData(int pos) {
        return centerList.get(pos);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int type) {

        if (type == HomeCenter.TYPE_HOME_HEAD) {
            View view = inflater.inflate(R.layout.item_home_center_head, viewGroup, false);
            return new HeadViewHolder(view, headListener);
        } else if (type == HomeCenter.TYPE_HOME_ASSET) {
            View view = inflater.inflate(R.layout.item_home_center_asset, viewGroup, false);
            return new AssetViewHolder(view);
        } else if (type == HomeCenter.TYPE_HOME_SCHEME) {
            View view = inflater.inflate(R.layout.item_home_center_scheme, viewGroup, false);
            return new SchemeViewHolder(view);
        } else {
            View view = inflater.inflate(R.layout.item_home_center_line, viewGroup, false);
            return new LineViewHolder(view);
        }

    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        HomeCenter center = centerList.get(i);

        if (viewHolder instanceof HeadViewHolder) {
            HeadViewHolder holder = (HeadViewHolder) viewHolder;
            if (holder.marqueeView.getNotices().isEmpty()) {
                holder.marqueeView.startWithList(center.list);
            }
        } else if (viewHolder instanceof AssetViewHolder) {
            AssetViewHolder holder = (AssetViewHolder) viewHolder;
            HomeAssetAdapter assetAdapter = new HomeAssetAdapter(context);
            assetAdapter.addData(center.uniAsset);
            holder.fullListView.setAdapter(assetAdapter);

            holder.switchView.setOpened(Prefs.with(context).readBoolean("HomeAsset", true));

            holder.switchView.setOnStateChangedListener(new SwitchView.OnStateChangedListener() {
                @Override
                public void toggleToOn(SwitchView view) {
                    HomeAssetAdapter adapter = (HomeAssetAdapter) holder.fullListView.getAdapter();
                    Prefs.with(context).writeBoolean("HomeAsset", true);
                    view.setOpened(true);
                    adapter.toggle(true);
                }

                @Override
                public void toggleToOff(SwitchView view) {
                    HomeAssetAdapter adapter = (HomeAssetAdapter) holder.fullListView.getAdapter();
                    Prefs.with(context).writeBoolean("HomeAsset", false);
                    view.setOpened(false);
                    adapter.toggle(false);
                }
            });

        } else if (viewHolder instanceof SchemeViewHolder) {
            SchemeViewHolder holder = (SchemeViewHolder) viewHolder;
            holder.schemeName.setText(center.name);
            holder.schemeIcon.setImageResource(center.icon);
            holder.schemeArrow.setImageResource(center.arrow);
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, center.toString(), Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return centerList.size();
    }

    /**
     * 选择隐藏分割线的条目
     *
     * @param position
     * @param parent
     * @return
     */
    @Override
    public boolean shouldHideDivider(int position, RecyclerView parent) {
//        if (position == 2) {
//            return true;
//        }
        return false;
    }

    //
//    @Override
//    public Paint dividerPaint(int position, RecyclerView parent) {
//        return null;
//    }
//
    @Override
    public int dividerLeftMargin(int position, RecyclerView parent) {
        return 32;
    }

    @Override
    public int dividerRightMargin(int position, RecyclerView parent) {
        return 32;
    }

    static class AssetViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.layout_asset)
        LinearLayout btnAsset;

        @BindView(R.id.v_switch)
        SwitchView switchView;

        @BindView(R.id.list_asset)
        FullListView fullListView;

        public AssetViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    static class HeadViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_pay)
        TextView pay;

        @BindView(R.id.txt_scan)
        TextView scan;

        @BindView(R.id.marqueeView)
        MarqueeView marqueeView;

        HeadListener listener;

        public HeadViewHolder(@NonNull View itemView, HeadListener listener) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.listener = listener;
        }

        @OnClick(R.id.txt_pay)
        void payClick() {
            listener.pay();
        }

        @OnClick(R.id.txt_scan)
        void scanClick() {
            listener.scan();
        }
    }

    static class SchemeViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_scheme_icon)
        ImageView schemeIcon;
        @BindView(R.id.txt_scheme_name)
        TextView schemeName;
        @BindView(R.id.img_scheme_arrow)
        ImageView schemeArrow;

        public SchemeViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    interface HeadListener {
        void scan();

        void pay();
    }


}
